import { VTActor } from "./VTActor";
import vtkPolyData from "@kitware/vtk.js/Common/DataModel/PolyData";
import vtkDataArray from "@kitware/vtk.js/Common/Core/DataArray";
import vtkScalarBarActor from "@kitware/vtk.js/Rendering/Core/ScalarBarActor";
import vtkLookupTable from "@kitware/vtk.js/Common/Core/LookupTable";
import { Vector3 } from "stl-typescript";

export enum ScalarType {
  POINT = "POINT",
  CELL = "CELL",
}

export class BdfActor extends VTActor {
  scalarRange = [0, 1];
  constructor(polyData: vtkPolyData, options = {}) {
    super(polyData, options);
    this.instance.getProperty().setLighting(false);
  }

  scalarBar0n() {
    const lut = vtkLookupTable.newInstance({
      numberOfColors: 256,
    });
    lut.build();
    this.mapper.setLookupTable(lut);
    this.mapper.setScalarVisibility(true);
    this.mapper.setScalarRange(this.scalarRange[0], this.scalarRange[1]);
    this.mapper.setScalarModeToUsePointData();
    this.mapper.setColorModeToMapScalars();
    this.mapper.modified();
    vtkScalarBarActor.newInstance();
    const scalarBarActor = vtkScalarBarActor.newInstance({
      automated: true,
      drawNanAnnotation: false,
    });
    scalarBarActor.setScalarsToColors(lut);
    scalarBarActor.setAxisTextStyle({
      fontColor: "black",
    });
    scalarBarActor.setTickTextStyle({
      fontColor: "black",
    });
    return scalarBarActor;
  }

  scalarBarOff() {}

  setScalars(data: number[], type: ScalarType = ScalarType.POINT) {
    if (type === ScalarType.POINT) {
      this.setPointsScalars(data);
    } else {
      // TODO: implement
    }
  }

  private setPointsScalars(data: number[]) {
    const npts = this.originData.getPoints().getNumberOfPoints();
    const scalars = vtkDataArray.newInstance({ size: npts });
    for (let i = 0; i < data.length / 3; i++) {
      const val = new Vector3(
        data[i * 3],
        data[i * 3 + 1],
        data[i * 3 + 2]
      ).length();
      scalars.setTuple(i, [val]);
    }
    this.scalarRange = scalars.getRange();
    this.originData.getPointData().setScalars(scalars);
  }
}
